package com.scau.yangsuiyu.estore.controller.buyer;

import com.scau.yangsuiyu.estore.controller.buyer.manageBasicInfo.InfoDeliveryAddressPane;
import com.scau.yangsuiyu.estore.controller.myComponent.AddressPicker;
import com.scau.yangsuiyu.estore.controller.myComponent.DeliveryAddressHBox;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.dao.DeliveryAddressDao;
import com.scau.yangsuiyu.estore.dababases.entity.DeliveryAddress;
import com.scau.yangsuiyu.estore.util.FormValidate;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 添加/修改收货地址的Pane
 * @date 2019-06-09 11:34
 * @email suiyu_yang@163.com
 */
public class EditDeliveryAddressPane extends AnchorPane implements Initializable {

    private InfoDeliveryAddressPane infoDeliveryAddressPane;
    private DeliveryAddressHBox deliveryAddressHBox;
    private FormValidate<TextField> formValidate = new FormValidate<>();
    private AddressPicker addressPicker;
    private Stage stage;
    private DeliveryAddress deliveryAddress; //当前正在修改或新增的收货地址

    @FXML
    private Label titleLabel;

    @FXML
    private TextField phoneTF;

    @FXML
    private TextField nameTF;

    @FXML
    private TextField zipcodeTF;

    @FXML
    private TextField moreAddressTF;

    @FXML
    private ComboBox<String> provinceCB;

    @FXML
    private ComboBox<String> cityCB;

    @FXML
    private ComboBox<String> districtCB;

    @FXML
    private Label errorLabel;

    public EditDeliveryAddressPane(InfoDeliveryAddressPane infoDeliveryAddressPane) { //新增
        this.infoDeliveryAddressPane = infoDeliveryAddressPane;
        this.deliveryAddress = new DeliveryAddress();
        loadFXML();
    }

    public EditDeliveryAddressPane(DeliveryAddressHBox deliveryAddressHBox) { //修改
        this.deliveryAddressHBox = deliveryAddressHBox;
        this.deliveryAddress = deliveryAddressHBox.getDeliveryAddress();
        loadFXML();
    }

    public EditDeliveryAddressPane() {
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().
                getResource("/view/buyer/EditDeliveryAddressPane.fxml"));

        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addressPicker = new AddressPicker(provinceCB, cityCB, districtCB);
        addressPicker.initComboBoxes();

        TextField[] mobileNoTFs = {phoneTF};
        Label[] regularLabels = {errorLabel};
        errorLabel.setText("手机号错误！");
        formValidate.installRegularValidation(mobileNoTFs, FormValidate.CHINAMOBILENO_REG, regularLabels);

    }

    private void modifyDeliveryAddress() {
        updateAddressFromUI();
        new DeliveryAddressDao().update(deliveryAddress);
        new BuyerDao().update(deliveryAddress.getBuyer());
    }

    private void addDeliveryAddress() {
        updateAddressFromUI();
        new DeliveryAddressDao().insert(deliveryAddress);
        new BuyerDao().update(infoDeliveryAddressPane.getBuyer());

    }

    /**
     * 根据当前UI更新Address
     *
     * @param
     */
    private void updateAddressFromUI() {
        deliveryAddress.setAddress(addressPicker.getAddressPickerString() + moreAddressTF.getText());
        deliveryAddress.setName(nameTF.getText());
        deliveryAddress.setZipcode(zipcodeTF.getText());
        deliveryAddress.setPhone(phoneTF.getText());

        if (infoDeliveryAddressPane != null) {
            deliveryAddress.setBuyer(infoDeliveryAddressPane.getBuyer());
        }
    }

    @FXML
    private void saveButtonClick() {
        if (!addressPicker.isAllChoose() || moreAddressTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入正确的收货地址！").showAndWait();
        } else if (nameTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入收货人姓名！").showAndWait();
        } else if (phoneTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入收货人手机号！").showAndWait();
        } else if (!formValidate.isValid()) {

        } else {
            if (infoDeliveryAddressPane != null) { //新增收货地址
                addDeliveryAddress();
                infoDeliveryAddressPane.updateAddress();
                stage.close();
            } else {  //修改收货地址
                modifyDeliveryAddress();
                deliveryAddressHBox.updateAddress();
                stage.close();
            }

        }
    }

    public void showPane() {
        stage = new Stage();
        stage.setScene(new Scene(this));

        if (infoDeliveryAddressPane != null) { //新增收货地址
            titleLabel.setText("新增收货地址");

        } else {  //修改收货地址
            titleLabel.setText("修改收货地址");
            setAddressToUI();
        }
        stage.show();
    }

    private void setAddressToUI() {
        if (deliveryAddress.getZipcode() != null) {
            zipcodeTF.setText(deliveryAddress.getZipcode());
        }
        nameTF.setText(deliveryAddress.getName());
        phoneTF.setText(deliveryAddress.getPhone());
    }
}
